@extends("layouts.backend.webadmin")

@section('content')
    <style>
        #searchCategory {
            border-radius: 0px;
        }
    </style>
    <link rel="stylesheet" href="{{env('APP_ROOT')}}/assets/select2/css/select2.min.css">
    <link rel="stylesheet" href="{{env('APP_ROOT')}}/assets/select2/css/select2totree.css">
    <link rel="stylesheet" href="{{env('APP_ROOT')}}/assets/layui/css/layui.css">
    <link rel="stylesheet" href="{{env('APP_ROOT')}}/css/vulslist.css">
    <script src="{{env('APP_ROOT')}}/assets/layui/layui.all.js"></script>
    <!-- Content Header (Page header) -->

    <!-- Main content -->
    <section class="content">
        <div class="row">
            <div class="col-xs-12">
                <div class="box">
                    <div class="box-header">

                    </div>
                    <!-- 搜索开始-->
                    <!-- 搜索结束-->
                    <!-- /.box-header -->
                    <div class="box-body1">
                        <table id="leveltable" class="table table-bordered table-striped">
                            <thead>
                            <tr>
                                {{--                            <th>#</th>--}}
                                <th>编号</th>
                                <th>标题</th>
                                <th>应用名称</th>
                                <th>所属分类</th>
                                <th>漏洞级别</th>
                                {{--                            <th>积分 / 安全币</th>--}}
                                <th>状态</th>
                                {{--功能暂不开放 --start --}}
                                {{--                            <th>是否删除</th>
                                                            <th>删除日期</th>--}}
                                {{--功能暂不开放 --end --}}
                                <th>操作</th>
                            </tr>
                            </thead>
                            <tbody>
                            @if(count($lists))
                            @foreach ($lists as $list)
                                <tr>
                                    {{--<td>{{$list->id}}</td>--}}
                                    <td>@if($list->vuls_number){{$list->vuls_number}}
                                        @endif</td>
                                    <td>{{$list->title}}</td>
                                    <td>{{$list->appname}}</td>
                                    <td>{{$list->cname}}</td>
                                    <td>{{$list->lname}}</td>
                                    <td>{{$vulStatus[$list->status][0]}}</td>
                                    <td>
                                            <a class="btn btn-success btn-sm disp distribute-distribute" name="sentencepoints"
                                               role="button" data-id="{{$list->vuls_id}}"><i
                                                        class="fa fa-pencil fa-fw"></i>分发</a>
                                        <a class="btn btn-success btn-sm"
                                           href="{{ URL::route('admin.distribute.det',['id'=>$list->vuls_id]) }}"
                                           role="button"><i class="fa fa-pencil fa-fw"></i>漏洞详情</a>
                                    </td>
                                </tr>
                            @endforeach
                            @else

                                <tr><td colspan="8" style="text-align: center">暂无数据！</td></tr>
                            @endif
                            </tbody>
                        </table>
                        @if(count($lists))
                        {{$lists->links()}}
                            @endif
                    </div>
                    <!-- /.box-body -->
                </div>
                <!-- /.box -->
            </div>
            <!-- /.col -->
        </div>

        <div id="distributeform" style="display: none">
           <div class="parents">
            <div style="height: 30px;width: 100%"></div>
            <div class="layui-form-item">
                <label class="layui-form-label">添加分发人员</label>
                <div class="layui-input-block">
                    <div class="input-group dropdown" style="width: 60%;">
                        {{--<span class="input-group-addon">$</span>--}}
                        <input type="text" name="title" required lay-verify="required" placeholder="请输入员工账号"
                               autocomplete="off" class="layui-input mysearch form-control">
                        <span class="input-group-addon check " data-toggle="dropdown" style="cursor:pointer;">验证</span>
                        <ul class="dropdown-menu" role="menu" aria-labelledby="dLabel">


                        </ul>
                    </div>

                </div>
            </div>


            <div style="height: 10px;border-bottom: dashed 1px #e2e2e2">
            </div>
            <div style="margin-left: 20px;margin-top: 20px;height:170px;">
                <div style="height: 20px;"><span class="label label-info">分发人员信息</span></div>
                <div class="addaccount" style="padding: 2px;">

                </div>
            </div>
            <div class="clearfix" style="height: 10px;border-bottom: dashed 1px #eee">
            </div>
            <div class="layui-form-item" >
                <div class="layui-input-block pull-right" style="padding: 10px;">
                    <input type="hidden" id="distributeId" value="">
                    <button onclick="submits(this)" class="btn btn-primary" lay-submit lay-filter="formDemo">立即提交
                    </button>
                </div>
            </div>

        </div>
        </div>
        <!-- 弹出层 分发结束-->
    </section>
    <!-- /.content -->
@stop

@section('javascript-end')
    <script src="{{env('APP_ROOT')}}/assets/select2/js/select2.min.js"></script>
    <script src="{{env('APP_ROOT')}}/assets/select2/js/select2totree.js"></script>
    <script>
        $('#searchCategory').select2ToTree({"border-radius": '0px'});
        $('#leveltable').DataTable({
            'paging': false,
            'lengthChange': false,
            'searching': false,
            'ordering': false,
            'info': false,
            'autoWidth': true,
            'pagingType': "simple_numbers",  //分页样式：simple,simple_numbers,full,full_numbers
            'oLanguage': {
                'sUrl': '{{env("APP_ROOT")}}/assets/bootstrap/js/zh_cn.json'
            }
        });


    </script>
    <script>
        //分发


        $('.disp').each(function () {
            $(this).click(function () {
                var vuls_id = $(this).attr('data-id');
                $('#distributeId').val(vuls_id);
                var html = ' <div class="pull-left" style="margin-top: 20px;margin-left: 20px;" >'
                var htmls = '<button type="button" onclick="deledistri(this)" class="close myclose pull-left" style="border:1px solid transparent;margin:6px ;" data-id="data_id_value" aria-hidden="true"><span class="label label-default">data_name</span>&times; </button>';

                $.ajax({
                    type: "POST",
                    data: {'vuls_id': vuls_id},
                    dataType: "json",
                    url: "{{route('admin.flaw.vuls_id')}}",
                    headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')},
                    success: function (data) {
                        var append = "";
                        console.log(data)
                        if (data.status == 2) {
                            $(data.data).each(function (index, element) {
                                var strings = htmls;
                                strings = strings.replace("data_id_value", element.users_id)
                                strings = strings.replace("data_name", element.name)
                                append += strings;
                            })
                            html += append
                            html += '<div class="clearfix"></div>'
                            html += '</div>'
                            $('#distributeform  .addaccount').html(html)
                            layer.open({
                                title: '分发',
                                area: ['700px', '400px'],
                                type: 1,
                                content: $('#distributeform').html(),
                                cancel: function (index) {
                                    $('#distributeform').hide();
                                    layer.close(index);
                                    return false;
                                },
                                success: function (layero, index) {
                                }

                            });

                        } else {
                            html += '<div class="clearfix"></div>'
                            html += '</div>'
                            $('#distributeform  .addaccount').html(html)
                            layer.open({
                                title: '分发',
                                area: ['600px', '400px'],
                                type: 1,
                                content: $('#distributeform').html(),
                                cancel: function (index) {
                                    $('#distributeform').hide();
                                    layer.close(index);
                                    return false;
                                },
                                success: function (layero, index) {
                                }
                            });
                        }
                    }
                });


            });
        });

        //查询相关信息
        $(document).on("click", ".check", function () {

            var p=$(this).parents(".parents")
            var length=p.find(".addaccount").find("button").length
            if(length>=10){
                layer.msg("最多可分发10个")
                return false
            }
            var check_value = $(this).parent(".dropdown").children("input");
            var ulli = $(this).parent(".dropdown").children("ul");
            ulli.html("")
            var values = check_value.val()   //要查询的人名
            if(!values){
                layer.msg("员工账号不能为空")
                return false
            }
            var index = layer.load(0, {shade: false}); //0代表加载的风格，支持0-2//loading层
            $.ajax({
                type: "POST",
                data: {'name': values},
                dataType: "json",
                url: "{{route('admin.flaw.getusers')}}",
                headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')},
                success: function (data) {
                    var htmls = '<li style="cursor:pointer;"><a onclick="details(this)"    data-user="data-account"                 data-name="data-vcname">data_user_info</a></li>';
                    var append = "";
                    if (data.status == 2) {
                        $(data.data).each(function (index, element) {
                            var strings = htmls;
                            var info = "" + element.vcName + "[" + element.vcAccount + "]" + "[" + element.vcEmail + "]" + "[" + element.vcFullName + "]";
                            strings = strings.replace("data-account", element.vcAccount)
                            strings = strings.replace("data-vcname", element.vcName)
                            strings = strings.replace("data_user_info", info)
                            append += strings;
                        })
                        layer.closeAll('loading');
                        ulli.html(append)
                    } else {
                        layer.closeAll('loading');
                        layer.msg(data.msg)
                    }
                }
            });
        })


        //        动态给生成的关闭按钮绑定事件

    </script>
    <script>
        /*查询出来的人员信息选择的时候
         * */
        function details(details) {
            var htmls = '<button type="button" onclick="deletes(this)" class="close adding myclose pull-left" style="border:1px solid transparent;margin:6px ;" data-ids="data_vac" data-user="data_van" aria-hidden="true"><span class="label label-default">data_name</span>&times; </button>';

            var vc_user = $(details).attr("data-user");
            var vc_name = $(details).attr("data-name");
            var showvalue = $(details).html()
            var parents = $(details).parent("li").parent("ul").parent(".dropdown");
            var p=$(details).parents(".parents")
            var strings = htmls;
            strings = strings.replace("data_name", vc_user)
            strings = strings.replace("data_vac", vc_user)
            strings = strings.replace("data_van", vc_name)
            parents.children("input").val("")
            p.find(".addaccount").find("button:last").next("div").remove()
            p.find(".addaccount").find("div.pull-left").append(strings)

        }

        //删除验证后的数据
        function deletes(deletes) {
            layer.confirm('确认要删除吗？', {
                btn: ['确定', '取消']//按钮
            }, function (index) {
                layer.close(index);
                $(deletes).remove()
                layer.msg("删除成功")
            })

        }


        function deledistri(distri) {
            layer.confirm('确认要删除吗？', {
                btn: ['确定', '取消']//按钮
            }, function (index) {
                layer.close(index);
                //此处请求后台程序，下方是成功后的前台处理……
                var id = $("#distributeId").val()
                var user = distri.getAttribute('data-id')
                var htmls = '<button type="button" onclick="deledistri(this)" class="close myclose pull-left" style="border:1px solid transparent;margin:6px ;" data-id="data_id_value" aria-hidden="true"><span class="label label-default">data_name</span>&times; </button>';
                var forms = $(distri).parent("div")
                var index = layer.load(0, {shade: false}); //0代表加载的风格，支持0-2//loading层
                $.ajax({
                    type: "POST",
                    data: {'vuls_id': id, 'user': user},
                    dataType: "json",
                    url: "{{route('admin.flaw.distridel')}}",
                    headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')},
                    success: function (data) {
                        var append = "";
                        if (data.status == 2) {
                            $(data.data).each(function (index, element) {
                                var strings = htmls;
                                strings = strings.replace("data_id_value", element.users_id)
                                strings = strings.replace("data_name", element.name)
                                append += strings;
                            })
                            forms.html(append)
                            layer.closeAll('loading');
                            layer.msg("删除成功")
                        } else {
                            layer.closeAll('loading');
                            layer.msg(data.msg)
                        }


                    },
                });
            });


        }
        ;

        /**
         * 提交
         * @param dom
         */
        function submits(dom) {
            var p=$(dom).parents(".parents")
            var buttons=p.find(".addaccount").find(".adding")
            var send="";
            var name="";
            $(buttons).each(function () {
                send+=$(this).attr("data-ids")+",";
                name+=  $(this).attr("data-user")+",";
            })
            if(!send){
                layer.msg("请添加待分发的人员信息")
            }
            var id = $("#distributeId").val()
            var aim =p.find(".addaccount")
            if (!id || !send ) {
                layer.msg("请添加待分发的人员信息")
                return false
            }
            var html = ' <div class="pull-left" style="margin-top: 20px;margin-left: 20px;" >'
            var htmls = '<button type="button" onclick="deledistri(this)" class="close myclose pull-left" style="border:1px solid transparent;margin:6px ;" data-id="data_id_value" aria-hidden="true"><span class="label label-default">data_name</span>&times; </button>';
            var index = layer.load(0, {shade: false}); //0代表加载的风格，支持0-2//loading层
            $.ajax({
                type: "POST",
                data: {'vuls_id': id, 'name': send},
                dataType: "json",
                url: "{{route('admin.flaw.distribute')}}",
                headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')},
                success: function (data) {
                    var append = "";
                    if (data.status == 2) {
                        $(data.data).each(function (index, element) {
                            var strings = htmls;
                            strings = strings.replace("data_id_value", element.users_id)
                            strings = strings.replace("data_name", element.name)
                            append += strings;
                        })
                        html += append
                        html += '<div class="clearfix"></div>'
                        html += '</div>'
                        aim.html(html)
                        layer.closeAll('loading');
                        layer.msg("分发成功")
                    } else {
                        layer.closeAll('loading');
                        layer.msg(data.msg)
                    }

                },

            });
        }

    </script>
@endsection